package lab03.test06;

import sun.awt.SunHints;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test06 {
    public static void main(String[] args) {
        String path = "D:\\word.txt";
        RandomAccessFile file = null;
        Map<String,Integer> map = new HashMap<String, Integer>();
        try {
            file = new RandomAccessFile(path,"r");
            //参数中第一个是文件的路径。第二个是文件的操作方式，r代表读文件

            String line = null;
            try {
                while ((line = file.readLine()) != null){
                    //先判断Map中是否有该单词，若没有就赋值为1
                    if (map.get(line) == null){
                        map.put(line,1);
                    }else {
                        map.put(line,map.get(line) +1);
                        /*int value = map.get(line);
                        value = value + 1;
                        map.put(line,value)
                         */
                    }
                    System.out.println(map.entrySet());
                }
                //map
                //key       value(出现的次数)
                //hello     2
                //world     1

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
